package com.caigua.mgp.mg.internal;

import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.internal.DefaultCommentGenerator;
import org.mybatis.generator.internal.util.StringUtility;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

/**
 * @BelongsProject: mybatis_generator_pluing
 * @BelongsPackage: com.caigua.mgp.mg.config
 * @Author: caigua
 * @CreateTime: 2020-12-09 00:05
 * @Description: todo
 */
public class CustomizeCommentGenerator extends DefaultCommentGenerator {

    private Properties properties = new Properties();

    private Properties systemPro = System.getProperties();

    private boolean suppressDate = false;

    private boolean suppressAllComments = true;

    private String currentDateStr = (new SimpleDateFormat("yyyy-MM-dd")).format(new Date());

    public void addJavaFileComment(CompilationUnit compilationUnit) {}

    public void addComment(XmlElement xmlElement) {}

    public void addRootComment(XmlElement rootElement) {}

    public void addConfigurationProperties(Properties properties) {
        this.properties.putAll(properties);
        this.suppressDate = StringUtility.isTrue(properties
                .getProperty("suppressDate"));
        this.suppressAllComments = StringUtility.isTrue(properties
                .getProperty("suppressAllComments"));
    }

    private void addJavadocTag(JavaElement javaElement) {
        Method method = null;
        if (javaElement instanceof Method)
            method = (Method)javaElement;
        StringBuilder sb = new StringBuilder();
        sb.append(" * ");
        if (method != null) {
            if (method.getParameters() != null && method.getParameters().size() > 0)
                for (Parameter parameter : method.getParameters()) {
                    sb.append("@param ");
                    sb.append(parameter.getName());
                    javaElement.addJavaDocLine(sb.toString());
                    sb.setLength(0);
                }
            if (method.getReturnType() != null && method
                    .getReturnType().getShortName() != null && method
                    .getReturnType().getShortName().trim().length() > 0) {
                sb.append(" * ");
                sb.append("@return ");
                sb.append(method.getReturnType().getShortName());
            }
        }
        String s = getDateString();
        if (s != null) {
            sb.append(' ');
            sb.append(s);
        }
        javaElement.addJavaDocLine(sb.toString());
    }

    public String getDateString() {
        String result = null;
        if (!this.suppressDate)
            result = this.currentDateStr;
        return result;
    }

    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {
        if (this.suppressAllComments)
            return;
        StringBuilder sb = new StringBuilder();
        innerClass.addJavaDocLine("/**");
        sb.append(" * ");
        sb.append(introspectedTable.getFullyQualifiedTable());
        sb.append(" ");
        sb.append(getDateString());
        innerClass.addJavaDocLine(sb.toString().replace("\n", " "));
        innerClass.addJavaDocLine(" */");
    }

    public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) {
        if (this.suppressAllComments)
            return;
        StringBuilder sb = new StringBuilder();
        innerEnum.addJavaDocLine("/**");
        sb.append(" * ");
        sb.append(introspectedTable.getFullyQualifiedTable());
        innerEnum.addJavaDocLine(sb.toString().replace("\n", " "));
        innerEnum.addJavaDocLine(" */");
    }

    public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
        if (this.suppressAllComments)
            return;
        StringBuilder sb = new StringBuilder();
        field.addJavaDocLine("/**");
        sb.append(" * ");
        sb.append(introspectedColumn.getRemarks());
        field.addJavaDocLine(sb.toString().replace("\n", " "));
        field.addJavaDocLine(" */");
    }

    public void addFieldComment(Field field, IntrospectedTable introspectedTable) {
        if (this.suppressAllComments)
            return;
        StringBuilder sb = new StringBuilder();
        field.addJavaDocLine("/**");
        sb.append(" * ");
        sb.append(introspectedTable.getFullyQualifiedTable());
        field.addJavaDocLine(sb.toString().replace("\n", " "));
        field.addJavaDocLine(" */");
    }

    public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {
        if (this.suppressAllComments)
            return;
        method.addJavaDocLine("/**");
        String methodName = method.getName();
        if ("insert".equalsIgnoreCase(methodName))
            method.addJavaDocLine(" * 新增");
        if ("delete".equalsIgnoreCase(methodName))
            method.addJavaDocLine(" * 根据id删除");
        if ("update".equalsIgnoreCase(methodName))
            method.addJavaDocLine(" * 根据id更新");
        if ("queryPage".equalsIgnoreCase(methodName))
            method.addJavaDocLine(" * 分页查询");
        if ("get".equalsIgnoreCase(methodName))
            method.addJavaDocLine(" * 根据id获取单条数据");
        if ("queryByIds".equalsIgnoreCase(methodName))
            method.addJavaDocLine(" * 根据id集合获取多条数据");
        addJavadocTag((JavaElement)method);
        method.addJavaDocLine(" */");
    }

    public void addGetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {}

    public void addSetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {}

    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) {
        if (this.suppressAllComments)
            return;
        StringBuilder sb = new StringBuilder();
        innerClass.addJavaDocLine("/**");
        sb.append(" * ");
        sb.append(introspectedTable.getFullyQualifiedTable());
        innerClass.addJavaDocLine(sb.toString().replace("\n", " "));
        sb.setLength(0);
        sb.append(" * @author ");
        sb.append(this.systemPro.getProperty("user.name"));
        sb.append(" ");
        sb.append(this.currentDateStr);
        innerClass.addJavaDocLine(" */");
    }
}
